<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class RssController extends Zend_Controller_Action 
{
	

	
	public function init(){
		
		
		
	}
	
	public function preDispatch()
	{

		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		$this->getHelper('Nav')->getHeaderNav();
		
		$this->getHelper('User')->getinfo($this->_request->getModuleName(),$this->_request->getControllerName());
		
	}

	
	public function makeAction(){
		ob_start(); 
		ignore_user_abort();
		set_time_limit(0);
		
		$this->_helper->viewRenderer->setNoRender();	

		$this->getHelper ('layout')->disableLayout ();
		
		$where="is_showed=1 and zcshop_products.onsales=0";
        $itemShow=new CItemShow();
        $itemShow->setType('product');
        $itemShow->setWhere($where);
        $itemCounts=$itemShow->getAllItemCounts();
        $itemShow->setFromTo(array('counts'=>200,'from'=>0));
        $myorder = R_PRE."items.add_time DESC";
        $itemShow->setOrder($myorder);
        $itemArray=$itemShow->getAllItems();
		
		$array=array(
	      'title'       => 'fancycheapjersey products', //required
	      'link'        => MY_URL.'/rss-index-rss.xml', //required
	      'charset'     => 'UTF-8', // required
	      'description' => 'fancycheapjersey.com soccer jersey shopping mall', //optional
	      //'author'      => 'housinghouse@live.cn', //optional
	      'copyright'   => 'fancycheapjersey.com', //optional
	      'generator'   => 'fancycheapjersey.com', // optional
	      'language'    => 'en-us', // optional
	      'ttl'         => '100', // optional, ignored if atom is used
	      'entries'     => array()
	       );
	       
	    foreach ($itemArray as $value){
	    	
	    	$array['entries'][]=array(
	                                   'title'        => str_replace('&','and',$value['subject']), //required
	                                   'link'         => $this->view->url($value['itemid']), //required
	                                   'description'  => preg_replace("/([^\x20-\x7E])/",' ',$value['description']), // only text, no html, required
	                                   'guid'         => $this->view->url($value['itemid']), //optional
	                                   'content'      => $this->view->toText(preg_replace("/([^\x20-\x7E])/",' ',$value['content'])), // can contain html, optional
	                                   'lastUpdate'   => $value['add_time'], // optional
	                                   );
	    	
	    }
	    
	    /**
	    $playerArray = CCache::getPlayerArray();
    	$this->view->playerArray = $playerArray;
    	$keyModel = new CKeywords();
    	foreach($playerArray as $playerInfo){
    		$realname = $playerInfo['name'];
    		$clubName = $playerInfo['club'];
    		$nationName = $playerInfo['national'];
    		$array['entries'][]=array(
	                                   'title'        => preg_replace("/([^\x20-\x7E])/",' ',$realname.' Soccer Jersey, '.$realname.' '.$clubName.' Soccer Jersey'), //required
	                                   'link'         => MY_URL."/player-soccer-jersey/".$keyModel->encode($realname), //required
	                                   'guid'         => MY_URL."/player-soccer-jersey/".$keyModel->encode($realname), //optional
	                                   'description'  => preg_replace("/([^\x20-\x7E])/",' ','Free Shipping on the '.$realname.' '.$clubName.' and '.$nationName.' Jerseys'),
	                                   'lastUpdate'   => time(), // optional
	                                   );
    	}
		
				
		//爬虫产品rss
		$crawl = new CCrawl();
		date_default_timezone_set('Asia/Chongqing'); //系统时间差8小时问题
		$curTime = strtotime(date('Y-m-d',time()));
		$daycounts = ($curTime-strtotime('2012-12-20'))/(3600*24);
		$offset = 122400+$daycounts*800;
		if($offset>=232275){
			$crawlRssProducts = $crawl->getRssCrawls(800);
		}else{
			$crawlRssProducts = $crawl->getRssCrawlsOffset(800,$offset);
		}
		foreach ($crawlRssProducts as $v) {
			$array['entries'][]=array(
	                                   'title'        => preg_replace("/([^\x20-\x7E])/",' ',str_replace('&','and',strip_tags($v['name']))), //required
	                                   'link'         => MY_URL.'/football_shirt_' . $v['id'] . '.html', //required
	                                   'guid'         => MY_URL.'/football_shirt_' . $v['id'] . '.html', //optional
	                                   'description'  => preg_replace("/([^\x20-\x7E])/",' ',str_replace('&','and',strip_tags($v['description']))),
	                                   'lastUpdate'   => time(), // optional
	                                   );			
		}
		
		//日志
		$item=new CItem();	
		$itemArray=$item->getAllItems(0,20,'blog');
		foreach ($itemArray as $v) {
			$array['entries'][]=array(
	                                   'title'        => preg_replace("/([^\x20-\x7E])/",' ',str_replace('&','and',strip_tags($v['subject']))), //required
	                                   'link'         => 'http://blog.fancycheapjersey.com/blog-detail-id-'.$v['itemid'], //required
	                                   'guid'         => 'http://blog.fancycheapjersey.com/blog-detail-id-'.$v['itemid'], //optional
	                                   'description'  => preg_replace("/([^\x20-\x7E])/",' ',str_replace('&','and',$this->subDesc($v['content']))),
	                                   'lastUpdate'   => time(), // optional
	                                   );			
		}
		**/
		
		$rssFeedFromArray = Zend_Feed::importArray($array, 'rss');
		
		$rssFeedFromArray->saveXml();
		
		$rssFeedFromArray->send();
		
		$buffer = ob_get_contents();
		
		file_put_contents(MY_ROOT.'/rss-index-rss.xml', $buffer); 

	}
	
	public function makenewsAction(){
		ob_start(); 
		ignore_user_abort();
		set_time_limit(0);
		
		$this->_helper->viewRenderer->setNoRender();	

		$this->getHelper ('layout')->disableLayout ();
		$item=new CBlog();	
		
		$itemArray=$item->getList(1,50)->toArray();
		
		$array=array(
	      'title'       => 'fancycheapjersey soccer news', //required
	      'link'        => MY_URL.'/soccer-news-rss.xml', //required
	      'charset'     => 'UTF-8', // required
	      'description' => 'fancycheapjersey.com soccer jersey news', //optional
	      //'author'      => 'housinghouse@live.cn', //optional
	      'copyright'   => 'fancycheapjersey.com', //optional
	      'generator'   => 'fancycheapjersey.com', // optional
	      //'language'    => 'en-us', // optional
	      'ttl'         => '100', // optional, ignored if atom is used
	      'entries'     => array()
	       );
	       
	    foreach ($itemArray as $value){
	    	
	    	$array['entries'][]=array(
	                                   'title'        => str_replace('&','and',$value['subject']), //required
	                                   'link'         => MY_URL . '/soccer-jersey-news-' . $value['blog_id'] . '-' . $this->view->encodeName($value['subject']), //required
	                                   'description'  => preg_replace("/([^\x20-\x7E])/",' ',$value['subject']), // only text, no html, required
	                                   'guid'         => MY_URL . '/soccer-jersey-news-' . $value['blog_id'] . '-' . $this->view->encodeName($value['subject']), //optional
	                                   'content'      => $this->view->toText(preg_replace("/([^\x20-\x7E])/",' ',$value['content'])), // can contain html, optional
	                                   'lastUpdate'   => $value['add_time'], // optional
	                                   );
	    	
	    }
	    
	    	
		$rssFeedFromArray = Zend_Feed::importArray($array, 'rss');
		
		$rssFeedFromArray->saveXml();
		
		$rssFeedFromArray->send();
		
		$buffer = ob_get_contents();
		
		file_put_contents(MY_ROOT.'/soccer-news-rss.xml', $buffer); 

	}
	
	private function subDesc($desc){
		if(mb_strlen($desc)<=200){
			return strip_tags(trim($desc));
		}
		$desc = mb_substr(strip_tags(trim($desc)),0,200);
		$position = mb_strrpos($desc,' ');
		return mb_substr($desc,0,$position).'...';
	}

	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
